import { editSrInfo, getDutyCourseDetail2, setHoliday } from "../../../../../utils/request/api";
import msg from "../../../../../utils/msg";
import { sr_data } from "../../util";
import User from "../../../../../utils/user";
var user = new User;
var times={
  '1':['2','3','4','5','6','7','8','9','10','11'],
  '2':['2','3','4','5','6','7','8','9','10','11'],
  '3':['3','4','5','6','7','8','9','10','11'],
  '4':['4','5','6','7','8','9','10','11'],
  '5':['5','6','7','8','9','10','11'],
  '6':['6','7','8','9','10','11'],
  '7':['7','8','9','10','11'],
  '8':['8','9','10','11'],
  '9':['9','10','11'],
  '10':['10','11'],
  '11':['11'],
};
var weeks={
  '1':['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '2':['2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '3':['3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '4':['4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '5':['5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '6':['6','7','8','9','10','11','12','13','14','15','16','17','18'],
  '7':['7','8','9','10','11','12','13','14','15','16','17','18'],
  '8':['8','9','10','11','12','13','14','15','16','17','18'],
  '9':['9','10','11','12','13','14','15','16','17','18'],
  '10':['10','11','12','13','14','15','16','17','18'],
  '11':['11','12','13','14','15','16','17','18'],
  '12':['12','13','14','15','16','17','18'],
  '13':['13','14','15','16','17','18'],
  '14':['14','15','16','17','18'],
  '15':['15','16','17','18'],
  '16':['16','17','18'],
  '17':['17','18'],
  '18':['18'],
};
// pages/user/s_worker/chooseTime/s_work2/s_work2.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: [] as sr_data[],
    signDetail: {} as sr_data,
    origin:{} as sr_data,
    marker: [],
    hasEdition:false,
    editWeek:false,//弹出层
    editWeeks:false,
    weeks:['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18'],
    editDay:false,
    days:['1','2','3','4','5','6','7'],
    editTime:false,
    columnTime: [
      {
        values: Object.keys(times),
        className: 'column1',
      },
      {
        values: times['1'],
        className: 'column2',
        defaultIndex: 2,
      },
    ],
    columnWeeks: [
      {
        values: Object.keys(weeks),
        className: 'column1',
      },
      {
        values: weeks,
        className: 'column2',
        defaultIndex: 2,
      },
    ],
    isEditAll:false,//此次更改是否更改所有该课程的课
    more:false,//更多操作
  },
  //只能一节课一节课改
  setHoliday:async function(){
    await msg.confirm('是否将这一节课登记为放假','点击确定该操作将立刻生效');
    msg.Loading('登记中');
    setHoliday({openid:user.openid,signDetail:JSON.stringify(this.data.signDetail)})
    .then((res:any)=>{
      msg.hideLoading();
      msg.show('设置成功','这一节课已经被登记为放假了');
      setTimeout(function(){
        wx.navigateBack();
      },2000)
    })
  },
  showMore(){
    this.setData({
      more:true,
    });
  },
  //设置签到点
  _setMarker(lat: number, lng: number) {
    const marker = {
      id: 0,
      width: '30px',
      height: '40px',
      latitude: lat,
      longitude: lng,
      label: {
        content: '签到点',  //文本
        color: '#FF0202',  //文本颜色
        borderRadius: 3,  //边框圆角
        borderWidth: 1,  //边框宽度
        borderColor: '#FF0202',  //边框颜色
        bgColor: '#ffffff',  //背景色
        padding: 5,  //文本边缘留白
        textAlign: 'center'  //文本对齐方式。有效值: left, right, center
      },
    }
    this.setData({ ['marker[0]']: marker });
  },
  onClickLeft() {
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {//{course:string,editAll:boolean,data:string}
  console.log('options=',options)
    let data = JSON.parse(options.data);
    if(options.editAll!=undefined){
      this.setData({
        isEditAll:true,
        signDetail:data,
        origin:JSON.parse(JSON.stringify(data)),
      });
    }
    else{
      this.setData({
        isEditAll:false,
        signDetail: JSON.parse(JSON.stringify(data)),
        origin:JSON.parse(JSON.stringify(data)),
      });
    }
    let user = new User;
    let param={openid:user.openid,c_day:data.c_day,c_time:data.c_time,course_id:data.course_id,week:data.week}
    getDutyCourseDetail2(param)
    .then((res:any)=>{
      this.setData({
        data:res.data.data,
        //data_origin:JSON.parse(JSON.stringify(res.data.data)),
      });
      this._setMarker(this.data.signDetail.latitude, this.data.signDetail.longitude);
      msg.hideLoading();
    })
  },
  /**
   * @function 提交修改
   */
  submit:async function() {
    if(!this.checkLegal()) return;
    let info = '此次更改只会修改这一次课';
    if(this.data.isEditAll) info="将会修改该课程所有的课！";
    await msg.confirm(info,'是否确认提交');
    let sr_id = [] as Array<number>;
    this.data.data.forEach((item) => {
      sr_id.push(item.sr_id);
    });
    let signDetail = JSON.stringify(this.data.signDetail);
    msg.Loading('更改中');
    await editSrInfo({signDetail:signDetail,sr_id:JSON.stringify(sr_id),openid:user.openid,editAll:this.data.isEditAll?1:0})
    .then((res:any)=>{
      msg.hideLoading();
      if(res.data.code=='200'){
        msg.show('更改成功','退出到“选择课程”后再进入该页面会刷新数据');
        setTimeout(function(){
        wx.navigateBack();},2000)
      }
      else if(res.data.code=='201'){
        msg.show(this.data.signDetail.c_name+"在第"+this.data.signDetail.week+"周星期"+this.data.signDetail.c_day+"第"+this.data.signDetail.c_time+"节已经有一节课了",'请换成一个不会撞课的时间段,如果这节课不上了，请在"更多操作"中登记为放假');
      }
      else msg.show('更改失败',res.data.data);
      
    })
    .catch((err:any)=>{console.log(err)});
    msg.hideLoading();
  },
  //选择签到点
  chooseLocation() {
    var self = this;
    wx.chooseLocation({
      success: function (lb) { 
        let latitude = lb.latitude;
        let longitude = lb.longitude;
        let signDetail=self.data.signDetail;
        signDetail.latitude=latitude;
        signDetail.longitude=longitude;
        self.setData({
          signDetail:signDetail,
          hasEdition:true,
        })
        self._setMarker(latitude,longitude);
      },
      complete: function (lb:any) {
      },
      cancel: function (lb:any) {
      },
      fail: function (lb) {
        console.log(lb);
        msg.show('获取签到地点失败','请重试');
      }
    });
  },
  onClose(){
    this.checkHasEdition();
    this.setData({
      editDay:false,
      editWeek:false,
      editTime:false,
      editWeeks:false,
      more:false,
    });
  },
  editWeeks(){
    this.setData({
      editWeeks:true,
    });
  },
  onChangeWeeks(event:any) {
    const { picker, value, index } = event.detail;
    let weeks_temp:any=weeks;
    picker.setColumnValues(1, weeks_temp[value[0]]);
    let v:string[] = value;
    let time = v[0]+'-'+(v[1]?v[1]:v[0]);
    let signDetail=this.data.signDetail;
    signDetail.c_weeks=time;
    this.setData({
      signDetail: signDetail,
    });
  },
  editWeek(){
    this.setData({
      editWeek:true,
    });
  },
  editDay(){
    this.setData({
      editDay:true,
    });
  },
  editTimes(){
    this.setData({
      editTime:true,
    });
  },
  onChangeWeek(e:any){
    this.setData({
      ['signDetail.week']: e.detail.value,
    });
  },
  onChangeDay(e:any){
    this.setData({
      ['signDetail.c_day']: e.detail.value,
    });
  },
  onChangeTime(event:any) {
    const { picker, value, index } = event.detail;
    let times_temp:any=times;
    picker.setColumnValues(1, times_temp[value[0]]);
    let v:string[] = value;
    let time = v[0]+'-'+v[1];
    let signDetail=this.data.signDetail;
    signDetail.c_time=time;
    this.setData({
      signDetail: signDetail,
    });
  },
  editLocation(e:any){
    let detail = e.detail.value;
    let signDetail=this.data.signDetail;
    signDetail.location=detail;
    this.setData({
      signDetail: signDetail,
    });
    this.checkHasEdition();
  },
  //检查是否有修改，有的话显示提交修改按钮
  //检查数据是否合法
  checkHasEdition(){
    let signDetail=this.data.signDetail;
    let origin:any=this.data.origin;
    //

    if(signDetail.latitude==origin.latitude
      &&signDetail.longitude==origin.longitude
      &&signDetail.week==origin.week
      &&signDetail.c_weeks==origin.c_weeks
      &&signDetail.c_day==origin.c_day
      &&signDetail.c_time==origin.c_time
      &&signDetail.location==origin.location
      &&signDetail.sign_start==origin.sign_start
      &&signDetail.sign_late_start==origin.sign_late_start
      &&signDetail.sign_end==origin.sign_end
      &&signDetail.radius==origin.radius){
        
        this.setData({
          hasEdition:false,
        });
      }
    else this.setData({
      hasEdition:true,
    })
  },
  checkLegal():boolean{
    //数据是否合法
    let signDetail=this.data.signDetail;
    let v=signDetail.c_weeks.split('-');
    if(Number(v[0])>Number(v[1])){
      msg.show('开始周大于结束周','错误，请重新输入');
      return false;
    }
    else if(Number(v[0])<=0||Number(v[1])<=0){
      msg.show('开始周或结束周小于等于0','错误，请重新输入');
      return false;
    }
    v = signDetail.c_time.split('-');
    if(Number(v[0])>Number(v[1])){
      msg.show('上课时间大于下课时间','错误，请重新输入');
      return false;
    }
    //
    if(isNaN(Number(signDetail.sign_end))){
        msg.show('签到结束时间请输入整数','');return false;
    }
    if(isNaN(Number(signDetail.sign_start))){
        msg.show('签到开始时间请输入整数','');return false;
    }
    if(isNaN(Number(signDetail.sign_late_start))){
        msg.show('迟到开始时间请输入整数','');return false;
    }
    if(isNaN(Number(signDetail.radius))){
        msg.show('签到半径请输入整数','');return false;
    }
    return true;
  },
  editSignStart(e:any){
    let detail = e.detail.value;
    let signDetail=this.data.signDetail;
    signDetail.sign_start=detail;
    this.setData({
      signDetail: signDetail,
    });
    this.checkHasEdition();
  },
  editSignLateStart(e:any){
    let detail = e.detail.value;
    let signDetail=this.data.signDetail;
    signDetail.sign_late_start=detail;
    this.setData({
      signDetail: signDetail,
    });
    this.checkHasEdition();
  },
  editSignEnd(e:any){
    let detail = e.detail.value;
    let signDetail=this.data.signDetail;
    signDetail.sign_end=detail;
    this.setData({
      signDetail: signDetail,
    });
    this.checkHasEdition();
  },
  editRadius(e:any){
    let detail = e.detail.value;
    let signDetail=this.data.signDetail;
    signDetail.radius=detail;
    this.setData({
      signDetail: signDetail,
    });
    this.checkHasEdition();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})